/*
    AuthorNote: ***
    Created By: Bambom Date:DATE
*/

using System;
using System.Diagnostics;

namespace AutoChess
{
    
    #region 状态和状态事件枚举
    /// <summary>
    /// 状态事件 作为状态转移的
    /// </summary>
    public enum ActorStateEvent
    {
        NullTransitionID = 0,
        
        //Common
        Actor_Move, //开始移动
        Actor_StopMove,//停止移动
        Actor_Arrived,//已到达
        
        //skill cast
        Actor_Skill_Cast, //释放技能
        Actor_Finish_Cast,//结束释放技能
        
        //stun
        Actor_Enter_Stun,//进入眩晕
        Actor_Finish_Stun,//退出眩晕
        
        //击倒
        Actor_HitDown,//进入击倒状态
        Actor_Finish_HitDown,//退出击倒
        
        //击飞
        Actor_HitFloat,//进入击飞
        Actor_Finish_HitFloat,//退出击飞
        
        Actor_Die,//角色死亡
        Actor_Relive,//角色复活
        
        Actor_Appearance,//棋子出场
        
        Actor_State_Max,
    }

    public enum ActorStateID
    {
        NullStateID = 0,
        Idle, //站立
        Move, //移动
        Skill,//技能状态
        Die,  //死亡
        Stun, //眩晕
        Frozen,//冰冻
        BattleFin,//战斗结束
        BattleCount,//战斗状态最大值
        
        Count,
    }
    #endregion

    class ActorSM : XStateMachine
    {
        public SMBaseState CurrentState
        {
            get { return _currentState; }
        }

        #region Initialization

        protected override void initiallizeStateMachine()
        {
            for (int i = 0; i < (int)ActorStateID.BattleCount; i++)
            {
                this.initOneState((ActorStateID) i);
            }
        }

        private SMBaseState initOneState(ActorStateID stateID)
        {
            SMBaseState _state = null;
            switch (stateID)
            {
                case ActorStateID.NullStateID:
                    break;
                case ActorStateID.Idle:
                    break;
                case ActorStateID.Move:
                    break;
                case ActorStateID.Skill:
                    break;
                case ActorStateID.Die:
                    break;
                case ActorStateID.Stun:
                    break;
                case ActorStateID.Frozen:
                    break;
                case ActorStateID.BattleFin:
                    break;
                case ActorStateID.BattleCount:
                    break;
                case ActorStateID.Count:
                    break;
                default:
                    throw new ArgumentOutOfRangeException("stateID", stateID, null);
            }

            _state.StateMachine = this;
            _state.ID = (int) stateID;
            _state.InitializedState();
            this.AddState(_state);
            
            return _state;
        }

        #endregion
        
        protected override void OnStart()
        {
            m_actor.Attr.SetAttribute<ActorStateID>(ActorActionAttrDef.CURRENT_STATE_ID,ActorStateID.Idle);
            AddEventListener<ActorStateEvent>(ActorEventType.ActorStateChange,OnChangeActorStateEvent);//通过事件将状态切换解耦
        }

        protected override void OnStateChange()
        {
            var attr = OwnActor.Attr;
            
            attr.SetAttribute(ActorActionAttrDef.CURRENT_STATE_ID,(ActorStateID)_currentStateID);
        }

        protected override void Update()
        {
            if (_currentState != null)
                _currentState.OnUpdate();
        }

        #region Callback for event

        /// <summary>
        /// 实际的装状态变更
        /// </summary>
        private void OnChangeActorStateEvent(ActorStateEvent actorStateEvent)
        {
            if (_currentState != null)
            {
                _currentState.OnReason((int)actorStateEvent);
            }
        }

        #endregion
    }
}